﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using ERF;
using FirstPerson.Gametypes;
using ERF.Menu;

namespace FirstPerson.States
{
    class PlayingMenuState : State
    {
        /// <summary>
        /// Singleton instance
        /// </summary>
        private static PlayingMenuState instance;

        /// <summary>
        /// Get an instance of this state.
        /// </summary>
        /// <param name="machine">the state machine that this state should belong to.</param>
        /// <returns></returns>
        public static PlayingMenuState GetInstance(StateMachine machine)
        {
            if (instance == null)
                instance = new PlayingMenuState(machine);

            return instance;
        }

        private MenuManager menuManager;

        /// <summary>
        /// Constructor of HostState
        /// </summary>
        /// /// <summary>
        /// <param name="machine">the state machine that this state should belong to.</param>
        /// <returns></returns>
        private PlayingMenuState(StateMachine machine)
            : base(machine)
        {
            this.menuManager = MenuManager.GetInstance(this.machine.CurrentGame);
        }

        public override void Initialize()
        {
            Menu menu = new Menu("Host Game");

            menu.MenuItems.Add(new MenuItem("Back to the match!", this.ContinueGame));
            menu.MenuItems.Add(new MenuItem("Options", this.GotoOptions));
            menu.MenuItems.Add(new MenuItem("Start menu", this.GotoStartMenu));
            menu.MenuItems.Add(new MenuItem("Quit game", this.QuitGame));
            menu.SelectedIndex = 0;

            menuManager.Menu = menu;
        }

        private void GotoStartMenu()
        {
            PlayingState playingState = PlayingState.GetInstance(base.machine);
            playingState.Hud.DeactivateHud();
            playingState.EndMatch();
            this.machine.CurrentGame.NetworkHandler.LeaveGame();

            machine.CurrentState = StartMenuState.GetInstance(machine);

        }

        private void ContinueGame()
        {
            machine.CurrentState = PlayingState.GetInstance(machine);
        }

        private void GotoOptions()
        {
            machine.CurrentState = OptionsState.GetInstance(machine);
        }

        private void QuitGame()
        {
            this.machine.CurrentGame.NetworkHandler.LeaveGame();
            this.machine.CurrentGame.Exit();
        }


        /// <summary>
        /// Update the state
        /// </summary>
        /// <param name="gameTime"></param>
        public override void Update(GameTime gameTime)
        {
            if (input.IsButtonReleased("MENU_DOWN"))
            {
                menuManager.Menu.SelectNext();
            }

            if (input.IsButtonReleased("MENU_UP"))
            {
                menuManager.Menu.SelectPrevious();
            }

            if (input.IsButtonReleased("MENU_SELECT"))
            {
                menuManager.Menu.Submit();
            }

            if (input.IsButtonReleased("MENU_BACK"))
            {
                if (menuManager.Menu.ParentList != null)
                    menuManager.Menu.Back();
                else
                    ContinueGame();
            }
        }
    }
}
